package com.wp.spring.controller;
import com.wp.spring.entity.User;
import com.wp.spring.exception.UserNotExistException;
import com.wp.spring.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wp.spring.bean.Person;

import java.util.List;

@Controller
public class CommonController {
    @Autowired
    private Person person;

    @Autowired
    private UserMapper userMapper;

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
//        if (1==1){
//            throw new UserNotExistException();
//        }
        Logger logger = LoggerFactory.getLogger(CommonController.class);
        //调用 sl4j 的 info() 方法，而非调用 logback 的方法
        logger.info("Hello World");


       System.out.println(person);


        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
       // Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);

       return person.toString();
    }

    @GetMapping(value = {"/testException"})
    public String testException(String user) {
        if ("user".equals(user)) {
            throw new UserNotExistException();
        }
        //跳转到登录页 login.html
        return "login";
    }

}
